var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');

var entryPath = path.resolve(__dirname, 'src/js/app.js');
var templatePath = __dirname + "/src/layout.html";
var port = "8083";

module.exports = {
    entry: entryPath,
    output: {
        filename: "./js/bundle.js"
    },

    module: {

        loaders: [
            {test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader'},
            {
                test: /\.css$/,
                exclude: /node_modules/,
                use: [
                    'style-loader',

                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1
                        }
                    },
                    {
                        loader: "postcss-loader",
                        options: {           // 如果没有options这个选项将会报错 No PostCSS Config found
                            plugins: (loader) => [
                                require('autoprefixer')() //CSS浏览器兼容
                            ]
                        }
                    }
                ]
            },
            {test: /\.(png|gif|jpg|jpeg|bmp)$/i, loader: 'url-loader?limit=5000&name=images/[name]-[hash:4].[ext]'},

            {
                //模板文件引入，以字符串的形式
                test: /\.html$/,
                loader: 'html-loader'
            }
        ]
    },


    plugins: [
        // html 模板插件
        new HtmlWebpackPlugin({
            template: templatePath,
            minify: {
                //去除注释 ，去除空格
                removeComments: true,
                collapseWhitespace: true
            }

        }),
        // 热加载插件
        new webpack.HotModuleReplacementPlugin(),

        // 打开浏览器
        new OpenBrowserPlugin({
            url: 'http://localhost:' + port
        }),

    ],

    devServer: {
        proxy: {
            // 凡是 `/api` 开头的 http 请求，都会被代理到 localhost:3000 上，由 koa 提供 mock 数据。
            // koa 代码在 ./mock 目录中，启动命令为 npm run mock
            '/mission/*': {
                //target    : 'http://localhost:8082',
                target: 'http://test.zhuandan.hua1000.com',
                changeOrigin: true,
                secure: false
            },
            '/api/*': {
                // target    : 'http://localhost:8082',
                target: 'http://test.xzlz.hua1000.com',
                changeOrigin: true,
                secure: false
            },
            '/userfiles/*': {
                // target    : 'http://localhost:8082',
                target: 'http://image.hua1000.com',
                changeOrigin: true,
                secure: false
            }
        },
        port: port,
        disableHostCheck: true,
        contentBase: "./src", //本地服务器所加载的页面所在的目录
        // colors: true, //终端中输出结果为彩色
        historyApiFallback: true, //不跳转
        inline: true, //实时刷新
        hot: true  // 使用热加载插件 HotModuleReplacementPlugin
    }
}
